<template>
  <el-container >
    <el-header class="no-flex" height="auto">
      <el-form inline :model="searchForm" label-width="80px">
        <el-form-item label="任务名称" class="inline" >
          <el-input v-model="searchForm.jobName" placeholder="请输入任务名称" :clearable="true" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="任务组名" class="inline">
          <el-select v-model="searchForm.jobGroup" placeholder="请选择任务组名" :clearable="true">
            <el-option v-for="item in dicts.system_global_taskgroup" :key="item.id" :label="item.k" :value="item.v"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="执行状态" class="inline">
          <el-select v-model="searchForm.status" placeholder="请选择任务状态" :clearable="true">
            <el-option v-for="item in dicts.system_task_status" :key="item.id" :label="item.k" :value="item.v"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间" class="inline" style="width: 400px">
          <el-date-picker style="width: 300px"
                          v-model="chooseTime"
                          type="datetimerange"
                          range-separator="-"
                          start-placeholder="开始"
                          end-placeholder="结束"
                          clearable
                          value-format="YYYY-MM-DD HH:mm:ss"
                          @change="handleChange"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  @click="upsearch">
            <span>搜索</span>
          </el-button>
          <el-button type="default" icon="el-icon-RefreshLeft" @click="reset">
            <span>重置</span>
          </el-button>
        </el-form-item>
        <el-form-item style="display: flex; margin-bottom: 0">
          <el-button type="danger" icon="el-icon-delete" @click="clear">清空</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main class="nopadding">
      <el-table ref="table" row-key="id" @selection-change="selectionChange" :data="tableData">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="任务名称" prop="jobName" width="100"></el-table-column>
        <el-table-column label="任务组名" prop="jobGroup" width="100"></el-table-column>
        <el-table-column label="调用字符串" prop="invokeTarget"></el-table-column>
        <el-table-column label="日志信息 " prop="jobMessage" width="250"></el-table-column>
        <el-table-column label="执行状态 " prop="createTime" width="100">
          <template #default="{row}">
            <template v-for="item in dicts.system_task_status">
              <el-tag v-if="row.status == item.v">{{item.k}}</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="开始时间 " prop="createTime" :formatter="rendererDateTime" width="100"></el-table-column>
        <el-table-column label="操作" fixed="right" align="right" width="250">
          <template #default="scope">
            <el-button-group>
              <el-button text type="primary" @click="show(scope.row.id, scope.$index)" icon="el-icon-View">详情</el-button>
              <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                <template #reference>
                  <el-button text type="danger" icon="el-icon-delete">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer style="display: flex;justify-content: flex-end">
      <el-pagination
        :current-page="searchForm.currentPage"
        :page-size="searchForm.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-footer>
  </el-container>
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './components/save.vue'
import ScTable from "../../../components/scTable/index.vue";
export default {
  name: 'timing',
  dicts:['system_global_taskgroup','system_task_status','system_global_yesorno'],
  components: {
    saveDialog,
  },
  data() {
    return {
      dialog: {
        save: false
      },
      total:0,
      chooseTime:null,
      searchForm:{
        current:1,
        size:10,
        jobName:'',
        jobGroup:'',
        status:null,
        beginTime:'',
        endTime:''
      },
      selection: [],
      search: {
        keyword: null
      },
      tableData:[],
      enabled:null,
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 时间选择
    handleChange(val){
      this.searchForm.beginTime= val[0]
      this.searchForm.endTime= val[1]
    },
    // 清空
    async clear(){
      let res = await this.$API.monitor.logger.clear.get()
      if(res.code == '200'){
        this.searchForm = []
        this.getData()
      }

    },
    // 请求数据
    getData(){
      this.$API.monitor.logger.list.get(this.searchForm).then(res=>{
        console.log(111,res)
        this.tableData  = res.data.records;
        this.total =res.data.total;
      })
    },
    //切换分页
    handleCurrentChange(e){
      this.searchForm.current = e;
      this.getData()
    },
    // 改变单页显示数量
    handleSizeChange(e){
      this.searchForm.size = e
      this.getData()
    },
    // 转换时间
    rendererDateTime(row, column, cellValue, index){
      return this.$TOOL.dateFormat(cellValue)
    },
    //添加
    // add(){
    //   this.dialog.save = true
    //   this.$nextTick(() => {
    //     this.$refs.saveDialog.open().setData()
    //   })
    // },
    //查看
    show(id){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('show').setData(id)
      })
    },
    //删除
    async table_del(id){
      let res = await this.$API.monitor.timing.del.post([id])
      if(res.code == 200){
        this.$message.success('删除成功')
        this.tableData = []
        this.getData()
      } else{
        this.$message.error('删除失败')
      }
    },
    //表格选择后回调事件
    selectionChange(selection){
      this.selection = selection;
    },
    //搜索
    upsearch(){
      this.getData()
    },
    //重置功能
    reset(){
      this.searchForm={
        current:1,
        size:10,
        jobName:'',
        jobGroup:'',
        concurrent:null,
        status:null
      }
      this.tableData=[];
      this.getData()
    },
    //本地更新数据
    handleSaveSuccess(data, mode){
      if(mode =='add'){
        this.reset()
      }else if(mode =='edit'){
        this.reset()
      }
    }
  }
}
</script>

<style scoped>

</style>
